/*
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.util.resource.RelativeResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import edu.ieslaferreria.creditsintesis.builders.CaveMazeTerrain;
import edu.ieslaferreria.creditsintesis.controllers.CollisionController;
import edu.ieslaferreria.creditsintesis.controllers.ExternalWorldController;
import edu.ieslaferreria.creditsintesis.controllers.InteractionController;
import edu.ieslaferreria.creditsintesis.controllers.SimpleCollisionController;
import edu.ieslaferreria.creditsintesis.controllers.UserActionController;
import edu.ieslaferreria.creditsintesis.logicgame.FirstExplorer;
import edu.ieslaferreria.creditsintesis.logicgame.Parameter;
import edu.ieslaferreria.creditsintesis.logicgame.actions.BackAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.ForwardAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RotateLeftAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RotateRightAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.SimpleRunner;
import edu.ieslaferreria.creditsintesis.logicgame.statemachineimpl.FirstExplorerStateMachine;
import edu.ieslaferreria.creditsintesis.models.NodeModel;
import edu.ieslaferreria.creditsintesis.models.TriMeshModel;
import edu.ieslaferreria.creditsintesis.rmi.PontServer;
import edu.ieslaferreria.creditsintesis.rmi.PontServerImpl;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Clase Main
 * con esto iniciamos el juego, el mapa, los
 * objetos del mapa y el jugador 1 que es el que hace de servidor
 * Hay varias cosas a modificar, cosas que 
 * podrian pasarse por parámetro, se podría cambiar para que no siempre el 
 * player 1 sea el servidor.
 * @author Ruben
 */
public class MainPlayer1Rmi extends MainPlayerRmi{
        private CollisionController collisionController;
        private PontServer serverGame;
        FirstExplorer firstExplorer1, firstExplorer2;
//        private String kindTerrain="PlaneTerrain";
        private String kindTerrain="CaveMazeTerrain";
//        private String kindTerrain="RandomTerrain";

	public static void main(String[] args) {
		// Signal to show properties dialog
		MainPlayer1Rmi app = new MainPlayer1Rmi(); // Create Object
		app.setConfigShowMode(ConfigShowMode.AlwaysShow);
		app.start(); // Start the program
	}

        @Override
    	protected void simpleInitGame() {
            display.setTitle("SocialSkills_P1");
//            kindTerrain = System.getProperty("kindTerrain", "PlaneTerrain");
            kindTerrain = System.getProperty("kindTerrain", "CaveMazeTerrain");
//            kindTerrain = System.getProperty("kindTerrain", "RandomTerrain");

            RelativeResourceLocator rrl = null;

            String pathForFlares= Main.class.getClassLoader().getResource("texture/flare1.png").getPath();
            pathForFlares = pathForFlares.substring(0, pathForFlares.lastIndexOf('/'));
            try {
                rrl = new RelativeResourceLocator(new URI("file://"+pathForFlares+"/"));
                Logger.getLogger(Main.class.getName()).log(Level.INFO, pathForFlares);
            } catch (URISyntaxException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, rrl);

//        System.setSecurityManager(new RMISecurityManager());

        try {
//            serverGame = new PontServerImpl(actionStates);
            
            createClient("Player1");

//            Pont p=serverGame.addClient(0, new PontImpl(client.getListener(), client.getNom()), "Player1",true);
//            client.setPont(p);



            serverGame = new PontServerImpl(actionStates,"Player1");
            serverGame.setListener(client.getListener());
            client.creaPartida(serverGame,false);
            PontServerImpl.registerToRegistry("ServerImpl", serverGame, true);

            buildControllers();
            buildWorld();
            buildObjects();
            buildChaseCamera();
            



        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }


        }

        private void buildWorld() throws RemoteException{
//            float map[]= new float[33*33];
            if(kindTerrain.equals("PlaneTerrain")){
                buildEnvironment(rootNode,"PlaneTerrain",17,33,new Vector3f(10f,10f,10f),"PlaneTerrain");
            }else if(kindTerrain.equals("CaveMazeTerrain")){
                buildEnvironment(rootNode,"CaveMazeTerrain",17,129,new Vector3f(20f,20f,20f),"CaveMazeTerrain");
            }else if(kindTerrain.equals("RandomTerrain")){
                buildEnvironment(rootNode,"RandomTerrain",17,129,new Vector3f(20f,20f,20f),"RandomTerrain");
            }
        }

        private void buildObjects() throws RemoteException{
            buildPlayer("Player1",new Vector3f(90,0, 190),0.03f,"model/tourist.obj","TouristModel",0f,2f);
            buildTree("FixedBox1", new Vector3f(-20,0,60));
            buildTree("FixedBox2", new Vector3f(15,0,90));
            buildTree("FixedBox3", new Vector3f(55,0,100));
            buildTree("FixedBox4", new Vector3f(60,0,125));
            buildTree("FixedBox5", new Vector3f(-125,0,-160));
            buildTree("FixedBox6", new Vector3f(15,0,-15));
            buildTree("FixedBox7", new Vector3f(-30,0,60));
            buildTree("FixedBox8", new Vector3f(-80,0,10));
            buildFirstExplorerObjects("Conejo1","rabbitModel","model/conejo.obj",0.04f,2f,0f,new Vector3f(190,0, 180));
            buildFirstExplorerObjects("Conejo2","rabbitModel","model/conejo.obj",0.04f,2f,0f,new Vector3f(-25,0, 25));
            buildFirstExplorerObjects("Conejo3","rabbitModel","model/conejo.obj",0.04f,2f,0f,new Vector3f(-80,0, -70));
        }
/**
     * set the basic parameters of the chase camera. This includes the offset. We want
     * to be behind the vehicle and a little above it. So we will the offset as 0 for
     * x and z, but be 1.5 times higher than the node.
     *
     * We then set the roll out parameters (2 units is the closest the camera can get, and
     * 5 is the furthest).
     *
     */



     /**
     * create our custom input handler.
     *
     */
    private void buildControllers() throws RemoteException {
        userActionController = new UserActionController(player, settings.getRenderer(), this.actionStates, input);
        collisionController = new SimpleCollisionController(this.actionStates);
        worldController = new ExternalWorldController(terrain, actionStates, collisionController);
        //worldController = new CaveWorldController(terrain, actionStates, collisionController);
        worldController.setAction(SimpleRunner.AC_GO_FORWARD, new ForwardAction());
        worldController.setAction(SimpleRunner.AC_SPIN_LEFT, new RotateLeftAction());
        worldController.setAction(SimpleRunner.AC_SPIN_RIGHT, new RotateRightAction());
        worldController.setAction(SimpleRunner.AC_GO_BACK, new BackAction());

        interactionController = new InteractionController(actionStates,client.getPont());


    }


}
